﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Hypermarket_Serial_Port
{
    class CashierRegister
    {
        private static string mCashierID;
        private static string mMachineID;
        private string mTransactionID;
        private DateTime mTransactionDate;
        private Dictionary<string, Transaction> mTransaction;
        private bool mIsTransactionStarted;
        private int mMissCount;
//        private string mLastProduct;
        private int mQuantity;

        public CashierRegister(string cashierID, string machineID)
        {
            mCashierID = cashierID;
            mMachineID = machineID;
            mIsTransactionStarted = false;
            mMissCount = 0;
//            mLastProduct = string.Empty;
        }

        public void StartTransaction(string transactionID)
        {
            mTransactionID = transactionID;
            mTransactionDate = DateTime.Now;
            mIsTransactionStarted = true;
            mTransaction = new Dictionary<string, Transaction>();
        }

        public void AddTransaction(string barcode, string name, string batchId, string costPrice, 
                                        string sellingPrice, string discount, int unitSold)
        {
            try
            {
                Transaction inTransaction = new Transaction();
                inTransaction.Barcode = barcode;
                inTransaction.ProductName = name;
                inTransaction.BatchID = batchId;
                inTransaction.CostPirce = costPrice;
                inTransaction.SellingPrice = sellingPrice;
                inTransaction.Discount = discount;
                inTransaction.UnitSold = unitSold;
                Transaction outTransaction;
                if(mTransaction.TryGetValue(barcode, out outTransaction))
                {
                    outTransaction.UnitSold = outTransaction.UnitSold + unitSold;
                    mTransaction[barcode] = outTransaction;
                }
                else
                {
                    mTransaction.Add(barcode, inTransaction);
//                    mLastProduct = barcode;
                }
            }
            catch
            {
                throw;
            }
        }

        public int getPreviousQuantity(string productID)
        {
            Transaction outTransaction;
            if (mTransaction.TryGetValue(productID, out outTransaction))
            {
                return outTransaction.UnitSold;
            }
            else
            {
                return Constant.ZERO;
            }
        }

        public Transaction getTransactionData(string productID)
        {
            Transaction outTransaction;
            mTransaction.TryGetValue(productID, out outTransaction);
            return outTransaction;
        }

        public bool CancelTransaction(string barcode)
        {
            try
            {
                bool isFound = mTransaction.Remove(barcode);
                //if (mLastProduct == barcode)
                //    mLastProduct = string.Empty;
                //isFound = mTransaction.Remove(mTransaction.Keys.Last());
                return isFound;
            }
            catch
            {
                throw;
            }
        }

        public bool CancelLastItem()
        {
            try
            {
                string lastProduct = mTransaction.Last().Key;
                bool isFound = mTransaction.Remove(lastProduct);
                return isFound;
            }
            catch
            {
                return false;
            }
            //bool isFound = mTransaction.Remove(mLastProduct);
            //mLastProduct = string.Empty;
            //return isFound;
        }

        public void CancelAllTransactions()
        {
            try
            {
                mIsTransactionStarted = false;
                mTransaction.Clear();
            }
            catch
            {
                throw;
            }
        }

        public List<Transaction> getTransactionList()
        {
            try
            {
                List<Transaction> transactionList = new List<Transaction>();
                foreach (KeyValuePair<string, Transaction> val in mTransaction)
                {
                    Transaction tr = val.Value;
                    //string query = "INSERT INTO CheckOutItem VALUES('" + mTransactionID + "','" + tr.Barcode + "','" +
                    //    tr.BatchID + "','" + tr.CostPirce + "," + tr.SellingPrice + "," + tr.Discount + "," + tr.UnitSold + ");";
                    transactionList.Add(tr);
                }
                return transactionList;
            }
            catch
            {
                throw;
            }
        }

        public bool CheckOut()
        {
            try
            {
                mTransaction.Clear();
                mIsTransactionStarted = false;
                return true;
            }
            catch
            {
                throw;
            }
        }

        public bool isTransactionStarted() { return mIsTransactionStarted; }
        public string getMachineID() { return mMachineID; }
        public string getCashierID() { return mCashierID; }
        public string getTransactionID() { return mTransactionID; }
        public DateTime getTransactionDate() { return mTransactionDate; }
//        public string getRequestLastItem() { return mLastProduct; }
        public string getRequestLastItem() 
        {
            try
            {
                return mTransaction.Last().Key;
            }
            catch
            {
                return string.Empty;
            }
        }
        public int getUnitSold(string productID)
        {
         
                Transaction tranS;
                if (mTransaction.TryGetValue(productID, out tranS))
                    return tranS.UnitSold;
                else
                    return Constant.ZERO;
        }

        public bool updateQuantity(string productID, int quantity)
        {
            Transaction outTransaction;
            try
            {
                mTransaction.TryGetValue(productID, out outTransaction);
                outTransaction.UnitSold -= quantity;
                return true;
            }
            catch
            {
                return false;
            }
        }



        public void ResetMissCount()
        {
            mMissCount = 0;
        }

        public void MissPolling()
        {
            mMissCount++;
        }

        public int GetMissCount()
        {
            return mMissCount;
        }

    }
}
